﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MedicWeb.forms;
using MedicWeb.MedicService;

namespace MedicWeb.Handlers {
    public class EditorHelper : SessionPage, IHttpHandler {
        
        private String getInfo(ObjectName objectName, string prefix) {
            string s = "\n" + prefix + "{\n";
            s += prefix + "\"object\":\"" + objectName.Object + "\",\n";
            s += prefix + "\"name\":\"" + objectName.Name + "\",\n";
            s += prefix + "\"children\":[";
            var children = new String[objectName.Children.Length];
            for (int i = 0; i < children.Length; i++) {
                children[i] = prefix + getInfo(objectName.Children[i], prefix + "\t");
            }
            s += String.Join(",", children);
            s += "\n" + prefix + "           ]";
            s += "\n" + prefix + "}";
            return s;
        }

        public new void ProcessRequest(HttpContext context) {
            string type = context.Request.QueryString["type"];
            context.Response.ContentType = "application/json";

            var ms = new MainService();
            var session = GetSession();
            var objectName = ms.GetObjectName(session, type);

            string s = getInfo(objectName, "");

            context.Response.Write(s);
            context.Response.Flush();
            context.Response.End();
        }

        public bool IsReusable {
            get { return true; }
        }
        
    }
}
